package com.yida.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.bean.BeanUtils;
import com.yida.system.api.domain.SysArea;
import com.yida.system.api.domain.SysOutsideUser;
import com.yida.system.api.domain.SysUser;
import com.yida.system.api.domain.SysUserCommunity;
import com.yida.system.service.ISysAreaService;
import com.yida.system.service.ISysOutsideUserService;
import com.yida.system.service.ISysUserCommunityService;

/**
 * @author hmh
 * @date 2023/04/11
 */
@Service
public class SysOutsideServiceImpl implements ISysOutsideUserService {

    // @Autowired
    // private ISysUserService iSysUserService;

    @Autowired
    private ISysAreaService areaService;

    @Autowired
    private ISysUserCommunityService iSysUserCommunityService;

    /* 
     * 通过用户名查询外网用户
     * 临时使用
     */
    @Override
    public SysOutsideUser selectOutsideUserByUserName(SysUser sysUser) {
        // SysUser sysUser = iSysUserService.selectUserByUserName(userName);
        SysOutsideUser sou = new SysOutsideUser();
        BeanUtils.copyBeanProp(sou, sysUser);
        // 获取地区组
        // 获取绑定地区
        List<SysArea> areaList = areaService.selectSysAreaByUserId(sou.getUserId());
        sou.setAreas(Optional.ofNullable(areaList).orElse(new ArrayList<SysArea>()).stream().map(SysArea::getAreaId).collect(Collectors.toList()));
        // 获取绑定街道和项目编号
        List<SysUserCommunity> communityList = Optional.ofNullable(iSysUserCommunityService.getBindCommunityByUserId(sou.getUserId())).orElse(new ArrayList<SysUserCommunity>());
        // 去除[null]空集合
        communityList.removeIf(Objects::isNull);
        if (StringUtils.isEmpty(communityList)) {
            throw new ServiceException("未获取到用户绑定项目信息，请绑定后再操作！");
        }
        // 获取街道组
        sou.setStreets(communityList.stream().map(SysUserCommunity::getStreetId).collect(Collectors.toList()));
        // 获取项目组
        sou.setCommunity(communityList.stream().map(SysUserCommunity::getCommunityId).collect(Collectors.toList()));
        return sou;
    }

    public static void main(String[] args) {
        List<SysUserCommunity> list = new ArrayList<SysUserCommunity>();
        // List<SysUserCommunity> list = null;
        // SysUserCommunity SysUserCommunity = new SysUserCommunity();
        // SysUserCommunity.setUserId(1L);
        SysUserCommunity SysUserCommunity = null;
        System.out.println(list);
        list.add(SysUserCommunity);
        // System.out.println(list.size());
        list.removeIf(Objects::isNull);
        if (StringUtils.isEmpty(list)) {
            System.out.println("1");
        }

        if (null == list || list.size() == 0) {
            System.out.println("11");
        } else {
            System.out.println("2");
        }

    }

}
